Optional

data class Optional<T>

This type represents T as not only potentially nullable data, but also as a data which can not be presented. This type will be useful in cases when T is nullable and null as valuable data too in time of data absence should be presented by some third type.

Let's imagine, you have nullable name in some database. In case when name is not nullable everything is clear - null will represent absence of row in the database. In case when name is nullable null will be a little bit dual-meaning, cause this null will say nothing about availability of the row (of course, it is exaggerated example)

See also

Types

Companion
Link copied to clipboard
object Companion

Extensions

dataOrElse
Link copied to clipboard
fun <T> Optional<T>.dataOrElse(block: () -> T): T

Returns Optional.data if Optional.dataPresented of this is true, or call block and returns the result of it

common
fun <T> Optional<T>.dataOrElse(block: () -> T): T

Returns Optional.data if Optional.dataPresented of this is true, or call block and returns the result of it

fun <T> Optional<T>.dataOrElse(block: () -> T): T

Returns Optional.data if Optional.dataPresented of this is true, or call block and returns the result of it

dataOrElseSuspendable
Link copied to clipboard
suspend fun <T> Optional<T>.dataOrElseSuspendable(block: suspend () -> T): T

Returns Optional.data if Optional.dataPresented of this is true, or call block and returns the result of it

common
suspend fun <T> Optional<T>.dataOrElseSuspendable(block: suspend () -> T): T

Returns Optional.data if Optional.dataPresented of this is true, or call block and returns the result of it

suspend fun <T> Optional<T>.dataOrElseSuspendable(block: suspend () -> T): T

Returns Optional.data if Optional.dataPresented of this is true, or call block and returns the result of it

dataOrNull
Link copied to clipboard
fun <T> Optional<T>.dataOrNull(): T?
fun <T> Optional<T>.dataOrNull(): T?
fun <T> Optional<T>.dataOrNull(): T?

Returns Optional.data if Optional.dataPresented of this is true, or null otherwise

dataOrThrow
Link copied to clipboard
fun <T> Optional<T>.dataOrThrow(throwable: Throwable): T

Returns Optional.data if Optional.dataPresented of this is true, or throw throwable otherwise

common
fun <T> Optional<T>.dataOrThrow(throwable: Throwable): T

Returns Optional.data if Optional.dataPresented of this is true, or throw throwable otherwise

fun <T> Optional<T>.dataOrThrow(throwable: Throwable): T

Returns Optional.data if Optional.dataPresented of this is true, or throw throwable otherwise

onAbsent
Link copied to clipboard
fun <T> Optional<T>.onAbsent(block: () -> Unit): Optional<T>

Will call block when data absent (Optional.dataPresented == false)

common
fun <T> Optional<T>.onAbsent(block: () -> Unit): Optional<T>

Will call block when data absent (Optional.dataPresented == false)

fun <T> Optional<T>.onAbsent(block: () -> Unit): Optional<T>

Will call block when data absent (Optional.dataPresented == false)

onPresented
Link copied to clipboard
fun <T> Optional<T>.onPresented(block: (T) -> Unit): Optional<T>

Will call block when data presented (Optional.dataPresented == true)

common
fun <T> Optional<T>.onPresented(block: (T) -> Unit): Optional<T>

Will call block when data presented (Optional.dataPresented == true)

fun <T> Optional<T>.onPresented(block: (T) -> Unit): Optional<T>

Will call block when data presented (Optional.dataPresented == true)